import 'package:chathouse/pages/client/home/service_detail.dart';
import 'package:chathouse/public/Config.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:chathouse/util/log.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../constant/app_constant.dart';
import '../../../model/client_search_model.dart';
import '../../route_manager/root_route_manager.dart';

/// @class : legal_engineering_page
/// @date : 2023-02-27 11:30
/// @name : LFC
/// @description : 法令檢測服務列表頁面
///

class LegalEngineeringPage extends StatefulWidget {
  const LegalEngineeringPage({Key? key}) : super(key: key);

  @override
  State<LegalEngineeringPage> createState() => _LegalEngineeringPageState();
}

class _LegalEngineeringPageState extends State<LegalEngineeringPage>
    with NavigationHelper {
  ClientSearchModel? clientSearchModel;
  final RefreshController _refreshController =
      RefreshController(initialRefresh: false); // 刷新控件控制

  ///下拉刷新
  void _onRefresh() {
    _requestData();
  }

  @override
  void initState() {
    super.initState();
    Obj.showLoading();
    _requestData();
  }

  void _requestData() {
    Apis.getLegalEngineerService().then((response) {
      Obj.dismissLoading();
      _refreshController.refreshCompleted();
      var json = response?.data;
      if (json == null) {
        return;
      }
      setState(() {
        clientSearchModel = ClientSearchModel.fromJson(json);
      });
    }).catchError((onError) {
      LogUtil.d("客户端獲取法令检测接口 onError: $onError");
      Obj.dismissLoading();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // backgroundColor: Colors.white,
        body: Container(
          decoration: const BoxDecoration(
              image: DecorationImage(
            image: AssetImage(Res.bg_law_list),
            fit: BoxFit.fill,
          )),
          child: Column(
            children: [
              Container(
                width: double.infinity,
                margin: EdgeInsets.only(top: 34.w),
                alignment: Alignment.center,
                child: Row(
                  children: [
                    ImageUtils.image(Res.icon_back, width: 26.w, height: 26.w)
                        .setContainer(margin: EdgeInsets.only(left: 22.w))
                        .setOnClickListener(() {
                      back();
                      // Get.back();
                    }, isInkWell: true),
                    Expanded(
                      child: Container(
                        alignment: Alignment.center,
                        child: Text(
                          "法令檢測/工程",
                          style: getTextStyle(40.sp,
                              color: Colors.white, fontType: FontType.BOLD),
                        ),
                      ),
                    ),
                    Container(
                      width: 26.w + 22.w,
                    )
                  ],
                ),
              ),
              Expanded(
                child: SmartRefresher(
                  enablePullDown: true,
                  controller: _refreshController,
                  onRefresh: _onRefresh,
                  child: GridView.builder(
                    //必須加以上兩句代碼 否則無法下拉刷新 //1;
                    shrinkWrap: true,
                    //跟随GridView内容变化高度//2;
                    physics: const BouncingScrollPhysics(),

                    padding: EdgeInsets.only(
                        left: 42.w, right: 42.w, top: 30.w, bottom: 22.w),
                    itemCount: clientSearchModel?.data?.length ?? 0,
                    gridDelegate:
                        const SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      mainAxisSpacing: 12,
                      crossAxisSpacing: 12,
                      childAspectRatio: 1,
                    ),
                    itemBuilder: (_, position) => buildItem(position),
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  Widget buildItem(index) {
    if (clientSearchModel == null) {
      return EmptyPage();
    }
    var widget = PhysicalModel(
      color: Colors.transparent,
      // shadowColor: Colors.black,
      elevation: 10.w,
      child: ClipRRect(
          borderRadius: BorderRadius.circular(10.w),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              AspectRatio(
                  aspectRatio: 154 / 103,
                  child: ImageUtils.image(
                      clientSearchModel?.data?[index].thumbnailImg ?? "",
                      imageType: ImageType.NETWORK,
                      fit: BoxFit.fill)),
              Expanded(
                flex: 1,
                child: Text(clientSearchModel?.data?[index].serviceName ?? "",
                    overflow: TextOverflow.ellipsis,
                    style: getTextStyle(
                      18.sp,
                      color: MyColors.zo_1299391,
                      fontType: FontType.BOLD,
                    )).setContainer(alignment: Alignment.center),
              )
            ],
          ).setContainer(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10.w)),
            ),
          )),
    ).setOnClickListener(() {
      toServeDetailPage(clientSearchModel?.data?[index].serviceId ??
          AppConstant.testWaterService);
      // Get.to(ServiceDetailPage(clientSearchModel?.data?[index].serviceId ??
      //     AppConstant.testWaterService));
    }, isInkWell: true);
    return widget;
  }

  void toServeDetailPage(num id) {
    // Get.to(ServiceDetailPage(id));
    navigateTo(ServiceDetailPage(id));
  }
}
